﻿using BlawkayEngine.ScreenSystem;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Net;
using BlawkayEngine;
using Microsoft.Xna.Framework.Input;
using BlawkayEngine.Input;

namespace BlawkayHockey
{
    public class LobbyScreen : Screen
    {
        public LobbyScreen(Game oGame)
            : base(oGame)
        {
            _BackgroundTexture = GameInfo.Texture_Square;
            _Font = oGame.Content.Load<SpriteFont>("Menu\\MenuOption");
            _ReadyTexture = GameInfo.Texture_Square;

            TransitionRate *= 8f;
        }

        public override void Update(ScreenManager oManager)
        {
            KeyboardInput oInput = GameInfo.KeyboardInput;

            if (Active)
            {
                if (oInput.JustPressed(Keys.LeftShift, Keys.RightShift))
                {
                    oManager.RemoveScreen(this);
                    _GoBackToXboxLive = true;

                    if (Networking.IsSessionRunning)
                        Networking.EndSession();
                }

                else if (oInput.JustPressed(Keys.R))
                {
                    if (Networking.IsSessionRunning)
                    {
                        Networking.ToggleReadyLocalPlayers();
                    }
                }

                //else if (oInput.JustPressed(Keys.R))
                //{
                //if (Networking.IsSessionRunning)
                //{
                if (_GoToGame == false && Networking.IsEverybodyReady())
                {
                    GameInfo.Debug.AddDebugMessage("Removing Screen, Destination Match");
                    oManager.RemoveScreen(this);
                    _GoToGame = true;
                }
                //}
                //}

            }
        }

        public override void Draw(ScreenManager oManager, SpriteBatch oSB)
        {
            if (Networking.IsSessionRunning == false)
                return;

            Rectangle oBackgroundRect = new Rectangle(200, 300, 400, 1);
            Vector2 oOrigin = Vector2.Zero;
            SpriteEffects oEffect = SpriteEffects.None;
            float oScale = 1.0f;
            float oRotation = 0.0f;

            if (Networking.IsEverybodyReady())
            {
                Rectangle oEverybodyReadRect = new Rectangle(oBackgroundRect.Left, oBackgroundRect.Top - 60, oBackgroundRect.Width, 50);
                oSB.Draw(_BackgroundTexture, oEverybodyReadRect, null, Color.Lime * Transition, oRotation, oOrigin, oEffect, 0.5f);
            }

            foreach (NetworkGamer oGamer in Networking.Session.AllGamers)
            {
                Vector2 oMeasure = _Font.MeasureString(oGamer.Gamertag);
                oBackgroundRect.Height = (int)oMeasure.Y + 6;
                Vector2 oOptionPosition = new Vector2(oBackgroundRect.Left + 5, oBackgroundRect.Top + 3);

                oSB.Draw(_BackgroundTexture, oBackgroundRect, null, Color.Black * 0.25f * Transition, oRotation, oOrigin, oEffect, 0.5f);
                oSB.DrawString(_Font, oGamer.Gamertag, oOptionPosition, Color.White * Transition, oRotation, oOrigin, oScale, oEffect, 1.0f);

                if (oGamer.IsReady)
                {
                    Rectangle oReadyRect = new Rectangle(oBackgroundRect.Left - 50, oBackgroundRect.Top, 50, 50);
                    oSB.Draw(_ReadyTexture, oReadyRect, null, Color.Lime * Transition, 0.0f, Vector2.Zero, SpriteEffects.None, 0.9f);
                }
                if (oGamer.IsHost)
                {
                    Rectangle oReadyRect = new Rectangle(oBackgroundRect.Right, oBackgroundRect.Top, 50, 50);
                    oSB.Draw(_ReadyTexture, oReadyRect, null, Color.Red * Transition, 0.0f, Vector2.Zero, SpriteEffects.None, 0.9f);
                }

                oBackgroundRect.Y = oBackgroundRect.Bottom + 5;
            }

        }

        public override void ActionWhenFullyAdded(ScreenManager oManager)
        {

        }

        public override void ActionWhenFullyRemoved(ScreenManager oManager)
        {
            if (_GoBackToXboxLive)
            {
                oManager.AddScreen(new CreateMatchScreen(oManager.Game));
            }
            if (_GoToGame)
            {
                GameInfo.Debug.AddDebugMessage("Screen Removed, Going to Match");

                GameInfo.ScreenManager = new MatchManager(oManager.Game);

                if (Networking.Session.IsHost)
                    Networking.Session.StartGame();
            }
        }

        bool _GoBackToXboxLive = false;
        bool _GoToGame = false;

        Texture2D _BackgroundTexture;
        Texture2D _ReadyTexture;
        SpriteFont _Font;

    }
}
